package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.MerchantFeePolicy;
import com.turing.post.organization.util.MerchantFeePolicyDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "organization.impl.MerchantFeePolicyDaoImpl")
@Remote
@Local
public class MerchantFeePolicyDaoImpl implements MerchantFeePolicyDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantFeePolicy> getMerchantFeePolicyByBusiness(
			MerchantFeePolicy mfp) {
		Query query = _em
				.createNativeQuery(
						"select t.* from MerchantFeePolicy t where "
								+ "t.MerchantNo = ?1 and t.Status in ( '1', '2' ) "
								+ "and t.TranType = ?2 "
								+ "and (t.BusinessType = ?3 or t.BusinessType is null) "
								+ "and (t.MrchBranchNo = ?4 or t.MrchBranchNo is null) "
								+ "and (t.CardFaceId = ?5 or t.CardFaceId is null) "
								+ "and (t.CardClassicId = ?6 or t.CardClassicId is null) "
								+ "and (t.StartCardSeqNo >= ?7 or t.StartCardSeqNo is null) "
								+ "and (t.EndCardSeqNo <= ?7 or t.EndCardSeqNo is null) "
								+ "and (t.StartDate >= to_date( ?8, 'yyyy-MM-dd' ) or t.StartDate is null) "
								+ "and (t.EndDate <= to_date( ?8, 'yyyy-MM-dd' ) or t.EndDate is null) "
								+ "and (t.StartAmount >= ?9 or t.StartAmount = 0) "
								+ "and (t.EndAmount <= ?9 or t.EndAmount = 0) "
								+ "order by t.AuthTime desc",
						MerchantFeePolicy.class);

		query.setParameter(1, mfp.getMerchantNo());
		query.setParameter(2, mfp.getTranType());
		query.setParameter(3, mfp.getBusinessType());
		query.setParameter(4, mfp.getMrchBranchNo());
		query.setParameter(5, mfp.getCardFaceId());
		query.setParameter(6, mfp.getCardClassicId());
		query.setParameter(7, mfp.getStartCardSeqNo());
		query.setParameter(8, mfp.getStartDate());
		query.setParameter(9, mfp.getStartAmount());

		return query.getResultList();
	}

	@Override
	public MerchantFeePolicy getMerchantFeePolicyByPolicyId(BigInteger mfpid) {
		return _em.find(MerchantFeePolicy.class, mfpid);
	}

	@Override
	public void insert(MerchantFeePolicy mfp) throws PCPSApplicationException {
		GeneralDao.insert(_em, mfp);
	}

	@Override
	public void update(MerchantFeePolicy mfp) throws PCPSApplicationException {
		GeneralDao.update(_em, mfp);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantFeePolicy> getMrchFeePolicyByConditions(
			PCPSWebInput webinput, String merchantNo, String businessType,
			String cardFaceId, String cardClassicId, String feeType,
			String status, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		if (webinput == null || webinput.getOperBranchNo() == null) {
			return null;
		}
		String issueCompanyNo = webinput.getOperBranchNo().substring(0, 6);
		String isssql = " t.merchantno=m.merchantno and m.issuecompanyno='"
				+ issueCompanyNo + "'";
		String sql = "select t.* from MerchantFeePolicy t,merchant m "
				+ "where " + isssql + "and (t.merchantNo = ?1 or ?1 is null)"
				+ "and (t.businessType = ?2 or ?2 is null) "
				+ "and (t.cardFaceId = ?3 or ?3 is null) "
				+ "and (t.cardClassicId = ?4 or ?4 is null) "
				+ "and (t.feeType = ?5 or ?5 is null) ";
		if (MerchantFeePolicy.STATUS_WAIT_AUTH.equals(status)) {
			sql += "and (t.status = '"
					+ MerchantFeePolicy.STATUS_INVALID_WAIT_AUTH
					+ "' or t.status = '"
					+ MerchantFeePolicy.STATUS_VALID_WAIT_AUTH + "') ";
		} else {
			if (!PCPSUtil.isEmpty(status)) {
				sql += "and t.status = '" + status + "'";
			}
		}
		sql += "order by t.CreateTime desc";
		Query query = _em.createNativeQuery(sql, MerchantFeePolicy.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);

		query.setParameter(1, merchantNo);
		query.setParameter(2, businessType);
		query.setParameter(3, cardFaceId);
		query.setParameter(4, cardClassicId);
		query.setParameter(5, feeType);

		return query.getResultList();
	}

	@Override
	public BigInteger getMrchFeePolicyTotalByConditions(PCPSWebInput webinput,
			String merchantNo, String businessType, String cardFaceId,
			String cardClassicId, String feeType, String status) {
		if (webinput == null || webinput.getOperBranchNo() == null) {
			return null;
		}
		String issueCompanyNo = webinput.getOperBranchNo().substring(0, 6);
		String isssql = " t.merchantno=m.merchantno and m.issuecompanyno='"
				+ issueCompanyNo + "'";
		String sql = "select nvl(count(*),0) from MerchantFeePolicy t,merchant m "
				+ "where "
				+ isssql
				+ "and (t.merchantNo = ?1 or ?1 is null)"
				+ "and (t.businessType = ?2 or ?2 is null) "
				+ "and (t.cardFaceId = ?3 or ?3 is null) "
				+ "and (t.cardClassicId = ?4 or ?4 is null) "
				+ "and (t.feeType = ?5 or ?5 is null) ";
		if (MerchantFeePolicy.STATUS_WAIT_AUTH.equals(status)) {
			sql += "and (t.status = '"
					+ MerchantFeePolicy.STATUS_INVALID_WAIT_AUTH
					+ "' or t.status = '"
					+ MerchantFeePolicy.STATUS_VALID_WAIT_AUTH + "') ";
		} else {
			if (!PCPSUtil.isEmpty(status)) {
				sql += "and t.status = '" + status + "'";
			}
		}
		Query query = _em.createNativeQuery(sql);

		query.setParameter(1, merchantNo);
		query.setParameter(2, businessType);
		query.setParameter(3, cardFaceId);
		query.setParameter(4, cardClassicId);
		query.setParameter(5, feeType);

		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void delete(MerchantFeePolicy mfp) throws PCPSApplicationException {
		GeneralDao.remove(_em, mfp);
	}

	@Override
	public MerchantFeePolicy getMerchantFeePolicyByPolicyIdForUpdate(
			BigInteger mfpid) {
		Query query = _em
				.createNativeQuery(
						"select * from MerchantFeePolicy t where t.mrchfeepolicyid=?1 for update",
						MerchantFeePolicy.class);
		query.setParameter(1, mfpid);
		return (MerchantFeePolicy) query.getSingleResult();
	}
}